﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tesis2.Models;

namespace Tesis2.Controllers
{
    public class DanoController : Controller
    {
        private TesisDBContext db = new TesisDBContext();

        //
        // GET: /Dano/

        public ActionResult Index()
        {
            var danos = db.Danos.Include(d => d.UnidadDano);
            return View(danos.ToList());
        }

        public ActionResult List()
        {
            var danos = db.Danos.Include(d => d.UnidadDano);
            return View(danos.ToList());
        }

        public ActionResult Search()
        {
            ViewBag.UnidadDanoID = new SelectList(db.Parametros, "ParametroID", "Nombre");
            return View();
        }

        //
        // GET: /Dano/Details/5

        public ActionResult Details(int id = 0)
        {
            Dano dano = db.Danos.Find(id);
            if (dano == null)
            {
                return HttpNotFound();
            }
            return View(dano);
        }

        //
        // GET: /Dano/Create

        public ActionResult Create()
        {
            ViewBag.UnidadDanoID = new SelectList((from item in db.Parametros
                                                   where item.CodigoUnico.Equals("UNIDADDANO")
                                                   select item), "ParametroID", "Valor").ToList();

            return View();
        }

        //
        // POST: /Dano/Create

        [HttpPost]
        public ActionResult Create(Dano dano)
        {
            if (ModelState.IsValid)
            {
                db.Danos.Add(dano);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.UnidadDanoID = new SelectList((from item in db.Parametros
                                                   where item.CodigoUnico.Equals("UNIDADDANO")
                                                   select item), "ParametroID", "Valor").ToList();
            return View(dano);
        }

        //
        // GET: /Dano/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Dano dano = db.Danos.Find(id);
            if (dano == null)
            {
                return HttpNotFound();
            }
            ViewBag.UnidadDanoID = new SelectList((from item in db.Parametros
                                                   where item.CodigoUnico.Equals("UNIDADDANO")
                                                   select item), "ParametroID", "Valor").ToList();
            return View(dano);
        }

        //
        // POST: /Dano/Edit/5

        [HttpPost]
        public ActionResult Edit(Dano dano)
        {
            if (ModelState.IsValid)
            {
                db.Entry(dano).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.UnidadDanoID = new SelectList((from item in db.Parametros
                                                   where item.CodigoUnico.Equals("UNIDADDANO")
                                                   select item), "ParametroID", "Valor").ToList();
            return View(dano);
        }

        //
        // GET: /Dano/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Dano dano = db.Danos.Find(id);
            if (dano == null)
            {
                return HttpNotFound();
            }
            return View(dano);
        }

        //
        // POST: /Dano/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Dano dano = db.Danos.Find(id);
            db.Danos.Remove(dano);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (db != null)
            {
                db.Dispose();
                base.Dispose(disposing);
            }
        }
    }
}